package com.beilang.hotline.title.manager.impl;

import java.util.ArrayList;
import java.util.List;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.CodeConstant;
import com.beilang.hotline.title.dao.TitleDAO;
import com.beilang.hotline.title.manager.TitleMgr;
import com.beilang.hotline.title.model.TitleModel;
import com.beilang.hotline.title.to.SearchTO;

public class TitleMgrImpl implements TitleMgr {

    private TitleDAO titleDAO;

    private CodeLookupMgr codeLookupMgr;

    public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
        this.codeLookupMgr = codeLookupMgr;
    }

    public void setTitleDAO(TitleDAO titleDAO) {
        this.titleDAO = titleDAO;
    }

    public TitleModel createTitle(TitleModel titleModel) {
        return titleDAO.createTitle(titleModel);
    }

    public TitleModel saveTitle(TitleModel titleModel) {
        return titleDAO.saveTitle(titleModel);
    }

    public void deleteTitle(Long titleId) {
        titleDAO.deleteTitle(titleId);
    }

    public TitleModel getTitle(Long titleId) {
        TitleModel titleModel = titleDAO.getTitle(titleId);
        if (titleModel!=null)
        {
            titleModel.setSysName(codeLookupMgr.getCodeDesc(CodeConstant.SYS_NAME, titleModel.getSysId().toString()));
            titleModel.setIssueName(codeLookupMgr.getCodeDesc(CodeConstant.ISSUE_LIST, titleModel.getIssueId().toString()));
        }
        return titleModel;
    }

    public PaginationHelper getTitleList(PaginationHelper helper, SearchTO searchTO) {
        int count = titleDAO.searchCount(searchTO);
        helper.setTotalSize(count);
        List list = titleDAO.searchList(helper, searchTO);
        if (list != null && list.size() > 0) {
            List l = new ArrayList();
            for (int i = 0; i < list.size(); i++) {
                TitleModel titleModel = (TitleModel) list.get(i);
                titleModel.setSysName(codeLookupMgr.getCodeDesc(CodeConstant.SYS_NAME, titleModel.getSysId().toString()));
                titleModel.setIssueName(codeLookupMgr.getCodeDesc(CodeConstant.ISSUE_LIST, titleModel.getIssueId().toString()));
                l.add(titleModel);
            }
            helper.setList(l);
        } else {
            helper.setList(list);
        }
        return helper;
    }

    public void deleteAllSysTitles(Long sysId) {
        titleDAO.deleteAllSysTitles(sysId);
    }

    public void deleteAllModuleTitles(Long moduleId) {
        titleDAO.deleteAllModuleTitles(moduleId);
    }
}
